package com.qding.park.manager.service.queue.biz;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.common.core.exceptions.business.adapter.AdapterException;
import com.qding.park.common.core.exceptions.business.manager.adapterfactory.AdapterProxyCreateFail;
import com.qding.park.manager.adapter.factory.AdapterParkProxyFactory;
import com.qding.park.manager.cache.ConfigDataCacheInter;
import com.qding.park.manager.pojo.TbInvitecard;
import com.qding.park.manager.pojo.TbParkspace;
import com.qding.park.manager.pojo.TbUserInfo;
import com.qding.park.manager.pojo.ap.ApVisitingReminder;
import com.qding.park.manager.pojo.enums.InviteStatusEnum;
import com.qding.park.manager.pojo.sms.device.InviteVisitNotice;
import com.qding.park.manager.service.queue.mapper.TbInvitecardMapper;
import com.qding.park.manager.service.queue.mapper.TbUserInfoMapper;
import com.qding.park.manager.sms.DevicePushInterfaceService;
import com.qding.park.target.common.adapter.InviteAdapter;
import com.qding.park.target.common.entity.AdInviteOrder;

@Component("inviteBiz")
public class InviteBiz {

    private static final Log _log = LogFactory.getLog(InviteBiz.class);

    @Autowired
    private TbInvitecardMapper tbInvitecardMapper;

    @Autowired
    private TbUserInfoMapper tbUserInfoMapper;

    @Autowired
    private ConfigDataCacheInter configDataCacheInter;

    @Autowired
    private DevicePushInterfaceService devicePushInterfaceService;

    @Autowired
    private AdapterParkProxyFactory adapterParkProxyFactory;

    @Transactional(rollbackFor = Throwable.class)
    public void inviteVisited(ApVisitingReminder apVisitingReminder) throws BaseException {
        // 供应商id
        String supplyId = apVisitingReminder.getSupplyID();
        // 车场方面的车辆编号
        String parkCode = apVisitingReminder.getParkId();
        // 卡号
        String cardCode = apVisitingReminder.getCardCode();
        // 车牌
        String carNum = apVisitingReminder.getCarNum();
        // 进场时间
        Date inDateTime = apVisitingReminder.getInDateTime();
        // 进场岗停
        String inRoom = apVisitingReminder.getInRoom();

        TbParkspace tbParkspace = configDataCacheInter.queryParkSpace(parkCode, Integer.valueOf(supplyId));
        int parkId = tbParkspace.getId();
        List<TbInvitecard> tbInvitecards = tbInvitecardMapper.queryInvitesByParkidCarnum(parkId, carNum);

        // 计算被几个人邀请了
        List<Integer> listTemp = new ArrayList<Integer>();
        for (int i = 0; i < tbInvitecards.size(); i++) {
            int userId = tbInvitecards.get(i).getUserID();
            if (!listTemp.contains(userId)) {
                listTemp.add(userId);
            }
        }

        if (listTemp.size() > 1) {
            // 有多人邀请
            // 更新邀请单
            for (TbInvitecard tbInvitecard : tbInvitecards) {
                tbInvitecard.setStatus(InviteStatusEnum.VISITED.getStatus());
                tbInvitecard.setCheckInTime(inDateTime);
                int num = tbInvitecardMapper.inviteVisited(tbInvitecard);
            }
        } else {
            // 只有一人邀请
            // 向邀请发起者推送 消息提醒
            /******* 通过北京接口实现消息提醒推送 *******************/
            // 更新邀请单
            for (TbInvitecard tbInvitecard : tbInvitecards) {
                tbInvitecard.setStatus(InviteStatusEnum.VISITED.getStatus());
                tbInvitecard.setCheckInTime(inDateTime);
                int num = tbInvitecardMapper.inviteVisited(tbInvitecard);

                /******* 通过北京接口实现消息提醒推送 *******************/
                InviteVisitNotice notice = new InviteVisitNotice();
                TbUserInfo userInfo = tbUserInfoMapper.queryUserById(tbInvitecard.getUserID());
                notice.setCarNum(carNum);
                notice.setMemberId(userInfo.getBjCode());
                devicePushInterfaceService.inviteVisitNotice(notice);
            }
        }

    }

    /**
     * @Description (TODO邀请车辆进场)
     * @param supplyId  厂商编号
     * @param parkCode  车场编号
     * @param carNum  车牌号
     * @param inDateTime  入场时间
     * @param inRoom  入场岗停
     * @throws BaseException
     */
    @Transactional(rollbackFor = Throwable.class)
    public void inviteVisitedArk(String supplyId, String parkCode, String carNum, Date inDateTime, String inRoom)
            throws BaseException {

        TbParkspace tbParkspace = configDataCacheInter.queryParkSpace(parkCode, Integer.valueOf(supplyId));
        int parkId = tbParkspace.getId();
        List<TbInvitecard> tbInvitecards = tbInvitecardMapper.queryInvitesByParkidCarnum(parkId, carNum);

        // 计算被几个人邀请了
        List<Integer> listTemp = new ArrayList<Integer>();
        for (int i = 0; i < tbInvitecards.size(); i++) {
            int userId = tbInvitecards.get(i).getUserID();
            if (!listTemp.contains(userId)) {
                listTemp.add(userId);
            }
        }

        if (listTemp.size() > 1) {
            // 有多人邀请
            // 更新邀请单
            for (TbInvitecard tbInvitecard : tbInvitecards) {
                tbInvitecard.setStatus(InviteStatusEnum.VISITED.getStatus());
                tbInvitecard.setCheckInTime(inDateTime);
                int num = tbInvitecardMapper.inviteVisited(tbInvitecard);
            }
        } else {
            // 只有一人邀请
            // 向邀请发起者推送 消息提醒
            /******* 通过北京接口实现消息提醒推送 *******************/
            // 更新邀请单
            for (TbInvitecard tbInvitecard : tbInvitecards) {
                tbInvitecard.setStatus(InviteStatusEnum.VISITED.getStatus());
                tbInvitecard.setCheckInTime(inDateTime);
                int num = tbInvitecardMapper.inviteVisited(tbInvitecard);

                /******* 通过北京接口实现消息提醒推送 *******************/
                InviteVisitNotice notice = new InviteVisitNotice();
                TbUserInfo userInfo = tbUserInfoMapper.queryUserById(tbInvitecard.getUserID());
                notice.setCarNum(carNum);
                notice.setMemberId(userInfo.getBjCode());
                devicePushInterfaceService.inviteVisitNotice(notice);
            }
        }

    }

    @Transactional(rollbackFor = Throwable.class)
    public void inviteTimeOut(TbInvitecard invitecard) throws AdapterProxyCreateFail, AdapterException {

        String id = invitecard.getId();
        int num = tbInvitecardMapper.updateStatusTimeOut(id, InviteStatusEnum.EXPIRED_VISITED.getStatus());
        if (num == 0) {
            _log.info("id:" + id + " 邀请单数据不一致");

            // return;
        }
        TbInvitecard tbInvitecard = tbInvitecardMapper.queryById(id);

        /******** 下发取消息邀请单操作 *****************/
        // TbParkspace tbParkspace = tbParkspaceMapper.queryParkInfoByParkId(tbInvitecard.getParkID());
        TbParkspace tbParkspace = configDataCacheInter.queryParkSpace(tbInvitecard.getParkID());
        String parkCode = tbParkspace.getParkCode();
        int supplierId = tbParkspace.getSupplierId();
        String carNum = tbInvitecard.getCarNum();
        InviteAdapter adapterProxy =adapterParkProxyFactory.getInviteAdapter(String.valueOf(supplierId));
        AdInviteOrder apInviteOrder = new AdInviteOrder();
        apInviteOrder.setOrderId(String.valueOf(id));
        apInviteOrder.setCarNum(tbInvitecard.getCarNum());
        apInviteOrder.setParkCode(parkCode);
        apInviteOrder.setStartTime(tbInvitecard.getStartTime());
        apInviteOrder.setEndTime(tbInvitecard.getEndTime());
        adapterProxy.cancelInvite(apInviteOrder);
        /******** 下发取消息邀请单操作 ****************/

    }

}
